package com.taomu.app.zongchouhuli.moudle.mine.contract.impl;

import com.alibaba.fastjson.JSONObject;
import com.taomu.app.zongchouhuli.entity.BaseChangeResponse;
import com.taomu.app.zongchouhuli.entity.BaseResponse;
import com.taomu.app.zongchouhuli.http.api.HttpObserver;
import com.taomu.app.zongchouhuli.http.api.Request;
import com.taomu.app.zongchouhuli.http.res.ResMyProductEntity;
import com.taomu.app.zongchouhuli.http.res.ResProductIncomeDetailEntity;
import com.taomu.app.zongchouhuli.moudle.mine.contract.UserIncomeDetailContract;
import com.taomu.app.zongchouhuli.moudle.mine.entity.IncomeDetailEnttiy;
import com.taomu.app.zongchouhuli.utils.ParseUtil;
import com.taomu.app.zongchouhuli.utils.StorageHelper;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;

public class UserIncomeDetailPresenter implements UserIncomeDetailContract.Presenter {

    private UserIncomeDetailContract.View mContractView;

    private CompositeDisposable disposable;

    public UserIncomeDetailPresenter(UserIncomeDetailContract.View mContractView){
        this.mContractView = mContractView;
        this.disposable = new CompositeDisposable();
    }

    @Override
    public void getProductInfo(String id) {
        Request.getIntence().getDistributeInfo(StorageHelper.getToken(),id)
                .flatMap(new Function<String, Observable<BaseChangeResponse<ResProductIncomeDetailEntity>>>() {
                    @Override
                    public Observable<BaseChangeResponse<ResProductIncomeDetailEntity>> apply(String json) throws Exception {
                        BaseResponse<JSONObject> baseResponse = ParseUtil.parseObject(json,BaseResponse.class);
                        BaseChangeResponse<ResProductIncomeDetailEntity> data = new BaseChangeResponse<>();
                        data.setData(ParseUtil.parseObject(baseResponse.getData().toString(),ResProductIncomeDetailEntity.class));
                        data.setCount(baseResponse.getCount());
                        return Observable.just(data);
                    }
                })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new HttpObserver<BaseChangeResponse<ResProductIncomeDetailEntity>>(mContractView,disposable,true) {
                    @Override
                    public void onNext(BaseChangeResponse<ResProductIncomeDetailEntity> baseResponse) {
                        if(mContractView != null){
                            mContractView.setProductInfo(baseResponse);
                        }
                    }
                });

    }

    @Override
    public void getIncomeDetailList() {
        setIncomeDetailList();
    }

    @Override
    public void detch() {

    }

    private String[] keys = {"产品名称","产品金额","本月收益","获益形式","购买时间"};

    private String[] values = {"众筹互利产品","500元","400元 ","推广会员佣金","2018-10-29 15:56"};

    private void setIncomeDetailList(){
        List<IncomeDetailEnttiy> mData = new ArrayList<>();
        for(int i=0;i<keys.length;i++){
            IncomeDetailEnttiy enttiy = new IncomeDetailEnttiy();
            enttiy.setKey(keys[i]);
            enttiy.setValue(values[i]);
            mData.add(enttiy);
        }
        if(mContractView != null){
            mContractView.setIncomeDetailList(mData);
        }
    }

    @Override
    public void onStart() {

    }

    @Override
    public void onDetch() {
        mContractView = null;
        if(disposable != null){
            disposable.dispose();
            disposable = null;
        }
    }
}
